CSRCS     = $(wildcard *.cpp)
CHDRS     = $(wildcard *.h)
COBJS     = $(addsuffix .o, $(basename $(CSRCS)))

EXTINCDIR = ../../include
DEPENDDIR = -I. -I$(EXTINCDIR)
LIBDIR    = ../../lib
EXTLINK   = $(PKGNAME).d
LIBDEPEND = $(LIBDIR)/lib.d

CXX       = g++
AR        = ar cr

CFLAGS = -g -Wall $(PKGFLAG)
#CFLAGS = -O3 -DNDEBUG -Wall $(PKGFLAG)

.PHONY: depend extheader

%.o : %.cpp 
	@echo "> compiling: $<"
	@$(CXX) $(CFLAGS) -I$(EXTINCDIR) -c -o $@ $<

top: $(EXTLINK) target
	@echo -n

clean:
	@rm -f $(COBJS)
#	@rm -f .depend.mak
#	@rm -f .extheader.mak

## Make dependencies
depend: .depend.mak
.depend.mak: $(CSRCS) $(CHDRS)
	@echo Making dependencies ...
	@$(CXX) -MM $(DEPENDDIR) $(CSRCS) > $@

## Linking external headers
extheader: .extheader.mak
.extheader.mak: $(EXTHDRS)
	@echo Linking external header files...
	@rm -f $@
	@echo -n "$(EXTLINK): " > $@
	@for hdr in $(EXTHDRS); \
	do \
		echo -n "$(EXTINCDIR)/$$hdr " >> $@; \
		rm -f $(EXTINCDIR)/$$hdr; \
	done
	@echo >> $@
	@for hdr in $(EXTHDRS); \
	do \
		echo "$(EXTINCDIR)/$$hdr: $$hdr" >> $@; \
		echo "	@rm -f $(EXTINCDIR)/$$hdr" >> $@; \
		echo "	@ln -fs ../src/$(PKGNAME)/$$hdr $(EXTINCDIR)/$$hdr" >> $@; \
	done

include .depend.mak
include .extheader.mak
